package app.netant.book.sjxs;

import org.jsoup.select.Elements;

import cn.lily.com.exception.YaconException;
import app.netant.NetAntBase;

public class SjxsDetail extends NetAntBase {
	
	private SjxsEntity entity;
	
	private String exp;

	public SjxsDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(SjxsEntity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		
		Elements activePart = document.select(".wrap.position span a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
		
	}
	
	public void analyseTitle(){
		Elements activePart = document.select(".wrap .show .showBox .detail .detail_info .detail_right h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
		}
	}
	
	public void analyseTag(){
		
		Elements activePart = document.select(".wrap .show .showBox .detail .detail_info .detail_right ul li");
		for(int i=0;i<activePart.size();i++){
			String tags = activePart.get(i).text();
			int idx = tags.indexOf("：");
			if(idx > 0){
				String tagName = tags.substring(0,idx).trim();
				String tagValue = tags.substring(idx+1).trim();
				System.out.println("【TAG】："+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
			}
		}
		
	}
	
	public void analyseMemo(){
		
		Elements activePart = document.select(".wrap .show .showBox .showInfo p");
		if(activePart.size()>0){
			String memo = this.getString(activePart.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
		
	}
	
	public void analyseHref(){
		
		Elements activePart = document.select(".wrap .show .showBox .showDown ul li a.downButton");
		for(int i=0;i<activePart.size();i++){
			String txt = activePart.get(i).text();
			String href = activePart.get(i).attr("href");
			if(txt.indexOf("下载")>0){
				
				System.out.println("【Href】:"+href);
				this.entity.setDownPath(href);
				
				int lastIdx = href.lastIndexOf(".");
				this.exp = href.substring(lastIdx);
				System.out.println("【EXP】："+this.exp);
				
				String name = this.entity.getNovelName();
				this.entity.setNovelFullName(name+this.exp);
				
				break;
			}
			
		}
		
		if(this.entity.getDownPath()==null||"".equals(this.entity.getDownPath())){
			throw new RuntimeException("无法获取下载地址信息！");
		}
		
	}
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseTag();
		this.analyseMemo();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		SjxsDetail detail = new SjxsDetail("https://www.sjxs.la/soft/1/2017/2017071685652.html");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseTag();
		detail.analyseMemo();
		detail.analyseHref();
		
	}
	
}
